﻿using CMS.DAL;
using CMS.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CMS.BLL
{
    public class BizBlog
    {
        private static int? MaxOrder()
        {
            using (var db = new DBContext())
            {
                var maxOrder = (from e in db.Blogs
                                select e.Order).Max();
                return maxOrder;
            }
        }
        public static bool Add(string name)
        {
            using (var db = new DBContext())
            {
                var maxOrder = MaxOrder();
                Blog blog = new Blog
                {
                    ID = Guid.NewGuid().ToString(),
                    Name = name,
                    Show = true,
                    Order = maxOrder != null ? (maxOrder.Value + 1) : 0
                };
                db.Blogs.Add(blog);
                db.SaveChanges();
                return true;
            }
        }
        public static bool Update(Blog _blog)
        {
            using (var db = new DBContext())
            {
                var blog = (from e in db.Blogs
                            where e.ID == _blog.ID
                            select e).SingleOrDefault();
                if (blog == null)
                    return false;
                blog.Name = _blog.Name;
                blog.Order = _blog.Order;
                blog.Show = _blog.Show;
                db.SaveChanges();
                return true;
            }
        }
        public static bool Delete(Blog _blog)
        {
            using (var db = new DBContext())
            {
                var blog = (from e in db.Blogs
                            where e.ID == _blog.ID
                            select e).SingleOrDefault();
                if (blog == null)
                    return false;
                if (blog.Posts.Count != 0)
                    return false;
                db.Blogs.Remove(blog);
                db.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="typeBlog">-1: Lấy blog không hiển thị. 0: Lấy tất cả. 1: Lấy blog hiển thị</param>
        /// <returns></returns>
        public static List<Blog> GetList(int typeBlog = 0)
        {
            using (var db = new DBContext())
            {
                if (typeBlog == 0)
                {
                    var list = from e in db.Blogs
                               select e;
                    return list.ToList();
                }
                else if (typeBlog == -1)
                {
                    var list = from e in db.Blogs
                               where e.Show == false
                               select e;
                    return list.ToList();
                }
                else if (typeBlog == 1)
                {
                    var list = from e in db.Blogs
                               where e.Show == true
                               select e;
                    return list.ToList();
                }
                else
                    throw new Exception("Tham số truyền vào không đúng");
            }
        }
        public static Blog Get(string id)
        {
            using (var db = new DBContext())
            {
                var blog = (from e in db.Blogs
                            where e.ID == id
                            select e).SingleOrDefault();
                return blog;
            }
        }
    }
}